import type { DeptDetailResult } from "@/api/types";
import type { UseBaseTable } from "@/composables";
import { deptApi } from "@/api";
import { useBaseTable } from "@/composables";
import { apiCall } from "@/utils/http/apiCall";

interface UseDeptTable extends Omit<UseBaseTable<DeptDetailResult>, "tablePage"> {
    /** 获取数据 */
    fetchTableData: (parentId?: string) => void;
}

export function useDeptTable(): UseDeptTable {
    const { tableLoading, tableData } = useBaseTable<DeptDetailResult>();

    function fetchTableData(parentId?: string): void {
        if (!parentId) {
            tableData.value = [];
            return;
        }
        tableLoading.value = true;
        apiCall(
            () => deptApi.listChildren(parentId),
            (data) => {
                tableData.value = data;
            },
            {
                onFinally: () => {
                    tableLoading.value = false;
                },
            },
        );
    }

    return {
        tableLoading,
        tableData,
        fetchTableData,
    };
}
